<template>
  <view class="page">
    <!-- 顶部导航 -->
    <view class="navbar">
      <view class="navbar-title">Mango音乐</view>
    </view>
    
    <!-- 内容区域 -->
    <view class="content">
      <!-- 搜索框 -->
      <view class="search-box">
        <input class="search-input" placeholder="搜索本地音乐" v-model="searchText" @input="filterMusic" />
        <view class="search-icon">🔍</view>
      </view>
      
      <!-- 本地音乐列表 -->
      <view class="section">
        <view class="section-title">本地音乐 ({{filteredMusicList.length}})</view>
        <view class="music-list">
          <view 
            v-for="(music, index) in filteredMusicList" 
            :key="music.id" 
            class="music-card"
            @tap="playMusic(index)"
          >
            <view class="music-cover">{{music.cover}}</view>
            <view class="music-info">
              <view class="music-title">{{music.title}}</view>
              <view class="music-artist">{{music.artist}}</view>
            </view>
            <view class="music-duration">{{music.duration}}</view>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view v-if="filteredMusicList.length === 0" class="empty-state">
        <view class="empty-icon">🎵</view>
        <view class="empty-text">暂无本地音乐</view>
        <view class="empty-tip">点击下方文件夹导入音乐</view>
      </view>
    </view>
    
    <!-- 播放控制栏 -->
    <view class="player-bar" v-if="currentMusic">
      <view class="player-info" @tap="openPlayerPage">
        <view class="player-title">{{currentMusic.title}}</view>
        <view class="player-artist">{{currentMusic.artist}}</view>
      </view>
      <view class="player-controls">
        <view class="player-btn" @tap="togglePlayPause">
          <text v-if="isPlaying">❚❚</text>
          <text v-else>▶</text>
        </view>
      </view>
    </view>
    
    <!-- 底部标签栏 -->
    <view class="tabbar">
      <view class="tab-item active">
        <view class="tab-icon">🎵</view>
        <view class="tab-text">音乐</view>
      </view>
      <view class="tab-item" @tap="importMusic">
        <view class="tab-icon">📁</view>
        <view class="tab-text">导入</view>
      </view>
      <view class="tab-item">
        <view class="tab-icon">⭐</view>
        <view class="tab-text">收藏</view>
      </view>
      <view class="tab-item">
        <view class="tab-icon">⚙️</view>
        <view class="tab-text">设置</view>
      </view>
    </view>
    
    <!-- 播放页面 -->
    <view class="player-page" v-if="showPlayerPage" @tap="closePlayerPage">
      <view class="player-content" @tap.stop>
        <view class="player-header">
          <view class="back-btn" @tap="closePlayerPage">←</view>
        </view>
        <view class="player-main">
          <view class="album-cover" :class="{ rotating: isPlaying }">{{currentMusic ? currentMusic.cover : '🎵'}}</view>
          <view class="player-song-info">
            <view class="player-song-title">{{currentMusic ? currentMusic.title : '歌曲名称'}}</view>
            <view class="player-song-artist">{{currentMusic ? currentMusic.artist : '歌手'}}</view>
          </view>
          <view class="progress-area">
            <view class="progress-bar">
              <view class="progress" :style="{width: progressPercent + '%'}"></view>
            </view>
            <view class="time-info">
              <span>{{formatTime(currentTime)}}</span>
              <span>{{formatTime(totalTime)}}</span>
            </view>
          </view>
          <view class="player-controls-large">
            <view class="control-btn" @tap="playPrev">⏮</view>
            <view class="control-btn" @tap="togglePlayPause">
              <text v-if="isPlaying">❚❚</text>
              <text v-else>▶</text>
            </view>
            <view class="control-btn" @tap="playNext">⏭</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 音乐数据
      musicList: [
        { id: 1, title: "夏天的风", artist: "周杰伦", duration: "3:45", cover: "🎵" },
        { id: 2, title: "夜曲", artist: "周杰伦", duration: "4:12", cover: "🎵" },
        { id: 3, title: "晴天", artist: "周杰伦", duration: "4:29", cover: "🎵" },
        { id: 4, title: "七里香", artist: "周杰伦", duration: "4:56", cover: "🎵" },
        { id: 5, title: "简单爱", artist: "周杰伦", duration: "4:30", cover: "🎵" }
      ],
      filteredMusicList: [],
      searchText: "",
      
      // 播放状态
      currentMusic: null,
      currentIndex: -1,
      isPlaying: false,
      showPlayerPage: false,
      
      // 播放进度
      currentTime: 0,
      totalTime: 180, // 3分钟
      progressPercent: 0,
      progressInterval: null
    }
  },
  
  onLoad() {
    this.filteredMusicList = [...this.musicList];
  },
  
  methods: {
    // 过滤音乐列表
    filterMusic() {
      if (!this.searchText) {
        this.filteredMusicList = [...this.musicList];
        return;
      }
      
      const searchText = this.searchText.toLowerCase();
      this.filteredMusicList = this.musicList.filter(music => 
        music.title.toLowerCase().includes(searchText) || 
        music.artist.toLowerCase().includes(searchText)
      );
    },
    
    // 播放音乐
    playMusic(index) {
      this.currentIndex = index;
      this.currentMusic = this.filteredMusicList[index];
      this.isPlaying = true;
      this.currentTime = 0;
      this.progressPercent = 0;
      
      this.startProgress();
    },
    
    // 播放/暂停切换
    togglePlayPause() {
      this.isPlaying = !this.isPlaying;
      
      if (this.isPlaying) {
        this.startProgress();
      } else {
        clearInterval(this.progressInterval);
      }
    },
    
    // 播放上一首
    playPrev() {
      if (this.currentIndex > 0) {
        this.playMusic(this.currentIndex - 1);
      } else {
        this.playMusic(this.filteredMusicList.length - 1);
      }
    },
    
    // 播放下一首
    playNext() {
      if (this.currentIndex < this.filteredMusicList.length - 1) {
        this.playMusic(this.currentIndex + 1);
      } else {
        this.playMusic(0);
      }
    },
    
    // 开始播放进度
    startProgress() {
      clearInterval(this.progressInterval);
      
      this.progressInterval = setInterval(() => {
        if (this.isPlaying && this.currentTime < this.totalTime) {
          this.currentTime += 1;
          this.progressPercent = (this.currentTime / this.totalTime) * 100;
        } else if (this.currentTime >= this.totalTime) {
          this.playNext();
        }
      }, 1000);
    },
    
    // 格式化时间显示
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60);
      const secs = seconds % 60;
      return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
    },
    
    // 打开播放页面
    openPlayerPage() {
      this.showPlayerPage = true;
    },
    
    // 关闭播放页面
    closePlayerPage() {
      this.showPlayerPage = false;
    },
    
    // 导入音乐
    importMusic() {
      uni.showActionSheet({
        itemList: ['从文件选择', '从文件夹选择'],
        success: (res) => {
          if (res.tapIndex === 0) {
            this.chooseMusicFile();
          } else {
            this.chooseMusicFolder();
          }
        }
      });
    },
    
    // 选择音乐文件
    chooseMusicFile() {
      // 实际项目中，这里应该调用uni.chooseFile API
      // 这里使用模拟数据
      uni.showLoading({
        title: '导入中...'
      });
      
      setTimeout(() => {
        const newMusic = {
          id: this.musicList.length + 1,
          title: `新歌曲${this.musicList.length + 1}`,
          artist: "未知艺术家",
          duration: "3:45",
          cover: "🎵"
        };
        
        this.musicList.push(newMusic);
        this.filteredMusicList = [...this.musicList];
        
        uni.hideLoading();
        uni.showToast({
          title: '导入成功',
          icon: 'success'
        });
      }, 1500);
    },
    
    // 选择音乐文件夹
    chooseMusicFolder() {
      uni.showModal({
        title: '导入音乐',
        content: '此功能需要访问文件系统权限，请在设置中开启权限后重试',
        showCancel: false
      });
    }
  },
  
  beforeDestroy() {
    clearInterval(this.progressInterval);
  }
}
</script>

<style>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

page {
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, sans-serif;
  background-color: #f5f5f5;
  color: #333;
  line-height: 1.5;
  overflow-x: hidden;
}

/* 主题色变量 */
:root {
  --primary-color: #FAD400;
  --primary-dark: #E6BF00;
  --text-color: #333;
  --text-light: #666;
  --bg-color: #f5f5f5;
  --card-bg: #fff;
  --border-color: #e0e0e0;
}

/* 页面容器 */
.page {
  max-width: 480px;
  margin: 0 auto;
  background-color: var(--card-bg);
  min-height: 100vh;
  position: relative;
  box-shadow: 0 0 10px rgba(0,0,0,0.05);
}

/* 顶部导航 */
.navbar {
  height: 44px;
  background-color: var(--primary-color);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  color: var(--text-color);
  font-weight: bold;
}

.navbar-title {
  font-size: 18px;
}

/* 内容区域 */
.content {
  padding: 15px;
  padding-bottom: 60px; /* 为底部标签栏留空间 */
}

/* 搜索框 */
.search-box {
  position: relative;
  margin-bottom: 15px;
}

.search-input {
  width: 100%;
  height: 36px;
  border-radius: 18px;
  border: 1px solid var(--border-color);
  padding: 0 40px 0 15px;
  font-size: 14px;
  background-color: #f9f9f9;
}

.search-icon {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-light);
}

/* 分区标题 */
.section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 12px;
  color: var(--text-color);
}

/* 音乐卡片 */
.music-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.music-card {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.05);
  display: flex;
  align-items: center;
}

.music-cover {
  width: 50px;
  height: 50px;
  border-radius: 6px;
  background-color: var(--primary-color);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-right: 12px;
}

.music-info {
  flex: 1;
}

.music-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 4px;
}

.music-artist {
  font-size: 14px;
  color: var(--text-light);
}

.music-duration {
  font-size: 14px;
  color: var(--text-light);
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-light);
}

.empty-icon {
  font-size: 60px;
  margin-bottom: 15px;
  color: var(--border-color);
}

.empty-text {
  font-size: 16px;
  margin-bottom: 8px;
}

.empty-tip {
  font-size: 14px;
}

/* 播放控制栏 */
.player-bar {
  position: fixed;
  bottom: 50px;
  left: 0;
  right: 0;
  height: 50px;
  background-color: var(--card-bg);
  display: flex;
  align-items: center;
  padding: 0 15px;
  border-top: 1px solid var(--border-color);
  z-index: 99;
  max-width: 480px;
  margin: 0 auto;
}

.player-info {
  flex: 1;
  overflow: hidden;
}

.player-title {
  font-size: 14px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-artist {
  font-size: 12px;
  color: var(--text-light);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-controls {
  display: flex;
  align-items: center;
}

.player-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: var(--primary-color);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-color);
  margin-left: 10px;
}

/* 底部标签栏 */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background-color: var(--card-bg);
  display: flex;
  border-top: 1px solid var(--border-color);
  z-index: 100;
  max-width: 480px;
  margin: 0 auto;
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--text-light);
}

.tab-item.active {
  color: var(--primary-color);
}

.tab-icon {
  font-size: 20px;
  margin-bottom: 2px;
}

.tab-text {
  font-size: 12px;
}

/* 播放页面 */
.player-page {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.player-content {
  width: 90%;
  max-width: 400px;
  background: linear-gradient(to bottom, var(--primary-color), #fff);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.player-header {
  height: 44px;
  display: flex;
  align-items: center;
  padding: 0 15px;
}

.back-btn {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
}

.player-main {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.album-cover {
  width: 250px;
  height: 250px;
  border-radius: 10px;
  background-color: rgba(255,255,255,0.3);
  margin-bottom: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  color: rgba(255,255,255,0.8);
  box-shadow: 0 10px 20px rgba(0,0,0,0.1);
  transition: transform 0.3s;
}

.rotating {
  animation: rotate 10s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.player-song-info {
  text-align: center;
  margin-bottom: 30px;
}

.player-song-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 8px;
}

.player-song-artist {
  font-size: 16px;
  color: var(--text-light);
}

.progress-area {
  width: 100%;
  margin-bottom: 30px;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background-color: rgba(0,0,0,0.1);
  border-radius: 2px;
  margin-bottom: 10px;
  position: relative;
}

.progress {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background-color: var(--text-color);
  border-radius: 2px;
  transition: width 0.3s;
}

.time-info {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-light);
}

.player-controls-large {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.control-btn {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin: 0 15px;
}

.control-btn:nth-child(2) {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background-color: var(--primary-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
}

/* 响应式调整 */
@media (max-width: 320px) {
  .content {
    padding: 10px;
    padding-bottom: 60px;
  }
  
  .album-cover {
    width: 200px;
    height: 200px;
  }
  
  .player-main {
    padding: 15px;
  }
}
</style>